/*
 * File: aw8692x.c
 *
 * Author: Ethan <renzhiqiang@awinic.com>
 *
 * Copyright (c) 2021 AWINIC Technology CO., LTD
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 */

// #include "main.h"
#include "haptic_hv.h"
#include "haptic_hv_reg.h"
#include "peripheral_i2c.h"

void aw8692x_raminit_enable(aw_bool);
void aw8692x_vbat_mode_config(aw_cont_vbat_comp);

struct aw_haptic_dts aw8692x_dts = {0};

// };

static struct aw_haptic_trig aw8692x_trig1;
//= {

//};

static struct aw_haptic_trig aw8692x_trig2;
//	= {

//};

static struct aw_haptic_trig aw8692x_trig3;
//	= {

//};

void aw8692x_struct_init(){
	aw8692x_dts.f0_pre = 2350;
	aw8692x_dts.d2s_gain = 0x04;
	aw8692x_dts.gain_bypass = 0x01;
	aw8692x_dts.f0_cali_percent = 7;
	aw8692x_dts.cont_tset = 0x06;
	aw8692x_dts.cont_drv1_lvl = 0x7f;
	aw8692x_dts.cont_lra_vrms = 1800;
	aw8692x_dts.cont_brk_time = 0x08;
	aw8692x_dts.cont_bemf_set = 0x02;
	aw8692x_dts.cont_wait_num = 0x06;
	aw8692x_dts.cont_brk_gain = 0x08;
	aw8692x_dts.cont_drv1_time = 0x04;
	aw8692x_dts.cont_drv2_time = 0x06;
	aw8692x_dts.cont_bst_brk_gain = 0x05;
	aw8692x_dts.cont_track_margin = 0x0f;
	aw8692x_dts.enabled_one_wire = AW_FALSE;
	
	aw8692x_trig1.trig_brk = 0;
	aw8692x_trig1.trig_bst = 0;
	aw8692x_trig1.trig_level = 0;
	aw8692x_trig1.trig_polar = 0;
	aw8692x_trig1.pos_enable = 0;
	aw8692x_trig1.neg_enable = 0;
	aw8692x_trig1.pos_sequence = 1;
	aw8692x_trig1.neg_sequence = 2;

aw8692x_trig2.trig_brk = 0;
aw8692x_trig2.trig_bst = 0;
aw8692x_trig2.trig_level = 0;
aw8692x_trig2.trig_polar = 0;
aw8692x_trig2.pos_enable = 0;
aw8692x_trig2.neg_enable = 0;
aw8692x_trig2.pos_sequence = 1;
aw8692x_trig2.neg_sequence = 2;

aw8692x_trig3.trig_brk = 0;
aw8692x_trig3.trig_bst = 0;
aw8692x_trig3.trig_level = 0;
aw8692x_trig3.trig_polar = 0;
aw8692x_trig3.pos_enable = 0;
aw8692x_trig3.neg_enable = 0;
aw8692x_trig3.pos_sequence = 1;
aw8692x_trig3.neg_sequence = 2;
}

void aw8692x_reg_unlock(aw_bool flag)
{
	uint8_t reg_val = 0;

	if (flag) {
		/* Unlock register */
		reg_val = AW8692X_BIT_TMCFG_TM_UNLOCK;
		AW86927_write_str(AW8692X_REG_TMCFG, &reg_val, AW_I2C_BYTE_ONE);
	} else {
		/* Lock register */
		reg_val = AW8692X_BIT_TMCFG_TM_LOCK;
		AW86927_write_str(AW8692X_REG_TMCFG, &reg_val, AW_I2C_BYTE_ONE);
	}
}

void aw8692x_raminit_enable(aw_bool flag)
{
	if (flag) {
		haptic_hv_i2c_write_bits(AW8692X_REG_SYSCTRL3, AW8692X_BIT_SYSCTRL3_EN_RAMINIT_MASK,
								 AW8692X_BIT_SYSCTRL3_EN_RAMINIT_ON);
	} else {
		haptic_hv_i2c_write_bits(AW8692X_REG_SYSCTRL3, AW8692X_BIT_SYSCTRL3_EN_RAMINIT_MASK,
								 AW8692X_BIT_SYSCTRL3_EN_RAMINIT_OFF);
	}
}

uint8_t reg_val[3] = {0};
int aw8692x_check_qualify(void)
{
	AW_LOGI("check qualify");
	AW86927_read_str(AW8692X_REG_EFCFG6, reg_val, AW_I2C_BYTE_THREE);
	if (!(reg_val[0] & (~AW8692X_BIT_EFCFG6_LOCK_MASK))) {
		AW_LOGE("unqualified chip!");
		return AW_ERROR;
	}
	haptic_hv_mdelay(1);
	aw8692x_reg_unlock(AW_TRUE);
	if ((reg_val[1] & (~AW8692X_BIT_EFCFG7_RESERVED_MASK)) ||
	    (reg_val[2] & (~AW8692X_BIT_EFCFG8_EF_TRM_BST_IPEAK_MASK))) {
		aw8692x_raminit_enable(AW_TRUE);
		reg_val[0] = AW8692X_BIT_EFCFG1_INIT_VAL;
		AW86927_write_str(AW8692X_REG_EFCFG1, reg_val, AW_I2C_BYTE_ONE);
		haptic_hv_mdelay(3);
		aw8692x_raminit_enable(AW_FALSE);
	}
	haptic_hv_mdelay(1);
	AW86927_read_str(AW8692X_REG_EFCFG7, &reg_val[1], AW_I2C_BYTE_TWO);
	if ((reg_val[1] & (~AW8692X_BIT_EFCFG7_RESERVED_MASK)) ||
	    (reg_val[2] & (~AW8692X_BIT_EFCFG8_EF_TRM_BST_IPEAK_MASK))) {
		return AW_ERROR;
	}
	reg_val[0] = AW8692X_BIT_ANACFG2_INIT_VAL;
	haptic_hv_mdelay(1);
	AW86927_write_str(AW8692X_REG_ANACFG2, reg_val, AW_I2C_BYTE_ONE);
	haptic_hv_mdelay(1);
	AW86927_read_str(AW8692X_REG_EFCFG9, &reg_val[0], AW_I2C_BYTE_ONE);
	if ((reg_val[0] & (~AW8692X_BIT_EFCFG9_EF_TRIM_OSC_MASK)) < AW8692X_BOOT_DETECTION)
		return AW_ERROR;
	haptic_hv_mdelay(1);
	aw8692x_reg_unlock(AW_FALSE);
	// while(1);
	return AW_SUCCESS;
}

void aw8692x_play_go(aw_bool flag)
{
	uint8_t go_on = AW8692X_BIT_PLAYCFG4_GO_ON;
	uint8_t stop_on = AW8692X_BIT_PLAYCFG4_STOP_ON;

	if (flag)
		AW86927_write_str(AW8692X_REG_PLAYCFG4, &go_on, AW_I2C_BYTE_ONE);
	else
		AW86927_write_str(AW8692X_REG_PLAYCFG4, &stop_on, AW_I2C_BYTE_ONE);
}

void aw8692x_set_pwm(aw_pwm pwm_type)
{
	switch (pwm_type) {
	case AW_PWM_48K:
		haptic_hv_i2c_write_bits(AW8692X_REG_SYSCTRL4, AW8692X_BIT_SYSCTRL4_WAVDAT_MODE_MASK,
								 AW8692X_BIT_SYSCTRL4_WAVDAT_48K);
		break;
	case AW_PWM_24K:
		haptic_hv_i2c_write_bits(AW8692X_REG_SYSCTRL4, AW8692X_BIT_SYSCTRL4_WAVDAT_MODE_MASK,
								 AW8692X_BIT_SYSCTRL4_WAVDAT_24K);
		break;
	case AW_PWM_12K:
		haptic_hv_i2c_write_bits(AW8692X_REG_SYSCTRL4, AW8692X_BIT_SYSCTRL4_WAVDAT_MODE_MASK,
								 AW8692X_BIT_SYSCTRL4_WAVDAT_12K);
		break;
	default:
		AW_LOGE("error param %d", pwm_type);
		break;
	}
}

void aw8692x_bst_mode_config(aw_bst_mode boost_mode)
{
	switch (boost_mode) {
	case AW_BST_BOOST_MODE:
		AW_LOGI("enter boost mode");
		haptic_hv_i2c_write_bits(AW8692X_REG_PLAYCFG1, AW8692X_BIT_PLAYCFG1_BST_MODE_MASK,
								 AW8692X_BIT_PLAYCFG1_BST_MODE);
		break;
	case AW_BST_BYPASS_MODE:
		AW_LOGI("enter bypass mode");
		haptic_hv_i2c_write_bits(AW8692X_REG_PLAYCFG1, AW8692X_BIT_PLAYCFG1_BST_MODE_MASK,
								 AW8692X_BIT_PLAYCFG1_BST_MODE_BYPASS);
		break;
	default:
		AW_LOGE("error mode = %d", boost_mode);
		break;
	}
}

void aw8692x_auto_brake_mode(aw_bool flag)
{
	if (flag) {
		haptic_hv_i2c_write_bits(AW8692X_REG_PLAYCFG3, AW8692X_BIT_PLAYCFG3_BRK_EN_MASK,
								 AW8692X_BIT_PLAYCFG3_BRK_ENABLE);
	} else {
		haptic_hv_i2c_write_bits(AW8692X_REG_PLAYCFG3, AW8692X_BIT_PLAYCFG3_BRK_EN_MASK,
								 AW8692X_BIT_PLAYCFG3_BRK_DISABLE);
	}
}

void aw8692x_play_mode(aw_mode mode)
{
	switch(mode) {
	case AW_STANDBY_MODE:
		g_haptic_hv->play_mode = AW_STANDBY_MODE;
		haptic_hv_i2c_write_bits(AW8692X_REG_SYSCTRL3, AW8692X_BIT_SYSCTRL3_STANDBY_MASK,
								 AW8692X_BIT_SYSCTRL3_STANDBY_ON);
		haptic_hv_i2c_write_bits(AW8692X_REG_SYSCTRL3, AW8692X_BIT_SYSCTRL3_STANDBY_MASK,
								 AW8692X_BIT_SYSCTRL3_STANDBY_OFF);
		AW_LOGI("enter standby mode");
		break;
	case AW_RAM_MODE:
		g_haptic_hv->play_mode = AW_RAM_MODE;
		haptic_hv_i2c_write_bits(AW8692X_REG_PLAYCFG3, AW8692X_BIT_PLAYCFG3_PLAY_MODE_MASK,
								 AW8692X_BIT_PLAYCFG3_PLAY_MODE_RAM);
		aw8692x_bst_mode_config(AW_BST_BOOST_MODE);
		aw8692x_vbat_mode_config(AW_CONT_VBAT_SW_COMP_MODE);
		AW_LOGI("enter ram mode");
		break;
	case AW_RTP_MODE:
		g_haptic_hv->play_mode = AW_RTP_MODE;
		haptic_hv_i2c_write_bits(AW8692X_REG_PLAYCFG3, AW8692X_BIT_PLAYCFG3_PLAY_MODE_MASK,
								 AW8692X_BIT_PLAYCFG3_PLAY_MODE_RTP);
		aw8692x_bst_mode_config(AW_BST_BOOST_MODE);
		aw8692x_vbat_mode_config(AW_CONT_VBAT_SW_COMP_MODE);
		AW_LOGI("enter rtp mode");
		break;
	case AW_TRIG_MODE:
		g_haptic_hv->play_mode = AW_TRIG_MODE;
		haptic_hv_i2c_write_bits(AW8692X_REG_PLAYCFG3, AW8692X_BIT_PLAYCFG3_PLAY_MODE_MASK,
								 AW8692X_BIT_PLAYCFG3_PLAY_MODE_RAM);
		aw8692x_vbat_mode_config(AW_CONT_VBAT_SW_COMP_MODE);
		AW_LOGI("enter trig mode");
		break;
	case AW_CONT_MODE:
		g_haptic_hv->play_mode = AW_CONT_MODE;
		haptic_hv_i2c_write_bits(AW8692X_REG_PLAYCFG3, AW8692X_BIT_PLAYCFG3_PLAY_MODE_MASK,
								 AW8692X_BIT_PLAYCFG3_PLAY_MODE_CONT);
		aw8692x_bst_mode_config(AW_BST_BYPASS_MODE);
		aw8692x_vbat_mode_config(AW_CONT_VBAT_HW_COMP_MODE);
		AW_LOGI("enter cont mode");
		break;
	case AW_RAM_LOOP_MODE:
		g_haptic_hv->play_mode = AW_RAM_LOOP_MODE;
		haptic_hv_i2c_write_bits(AW8692X_REG_PLAYCFG3, AW8692X_BIT_PLAYCFG3_PLAY_MODE_MASK,
								 AW8692X_BIT_PLAYCFG3_PLAY_MODE_RAM);
		aw8692x_bst_mode_config(AW_BST_BYPASS_MODE);
		aw8692x_vbat_mode_config(AW_CONT_VBAT_SW_COMP_MODE);
		AW_LOGI("enter ram loop mode");
		break;
	default:
		AW_LOGE("error mode");
		break;
	}
}

uint8_t aw8692x_get_glb_state(void)
{
	uint8_t state = 0;

	AW86927_read_str(AW8692X_REG_GLBRD5, &state, AW_I2C_BYTE_ONE);
	return state;
}

int aw8692x_wait_enter_standby(void)
{
	uint8_t reg_val = 0;
	int count = 100;

	while (count--) {
		reg_val = aw8692x_get_glb_state();
		if (reg_val == AW8692X_BIT_GLBRD5_STATE_STANDBY) {
			AW_LOGI("entered standby!");
			return AW_SUCCESS;
		}
		haptic_hv_mdelay(2);
	}
	AW_LOGE("do not enter standby automatically");
	return AW_ERROR;
}

void aw8692x_play_stop(void)
{
	uint8_t reg_val = 0;
	int ret = 0;

	g_haptic_hv->play_mode = AW_STANDBY_MODE;
	reg_val = AW8692X_BIT_PLAYCFG4_STOP_ON;
	AW86927_write_str(AW8692X_REG_PLAYCFG4, &reg_val, AW_I2C_BYTE_ONE);
	ret = aw8692x_wait_enter_standby();
	if (ret == AW_ERROR) {
		AW_LOGE("force to enter standby mode!");
		aw8692x_play_mode(AW_STANDBY_MODE);
	}
}

int aw8692x_judge_rtp_going()
{
	uint8_t glb_state = 0;
	uint8_t rtp_state = 0;

	glb_state = aw8692x_get_glb_state();
	if (glb_state == AW8692X_BIT_GLBRD5_STATE_RTP_GO) {
		rtp_state = 1;  /*is going on */
		AW_LOGI("rtp_routine_on");
	}
	return rtp_state;
}

void aw8692x_cont_config(void)
{
	/* uint8_t drv1_time = 0xFF; */
	uint8_t drv2_time = 0xFF;
	uint8_t cont_config[2] = {0};

	AW_LOGI("enter");
	/* work mode */
	aw8692x_play_mode(AW_CONT_MODE);

	cont_config[0] = aw8692x_dts.cont_drv1_lvl | AW8692X_BIT_CONTCFG6_TRACK_ENABLE;
	cont_config[1] = aw8692x_dts.cont_drv2_lvl;
	AW86927_write_str(AW8692X_REG_CONTCFG6, cont_config, AW_I2C_BYTE_TWO);

	/* DRV1_TIME */
	/* haptic_hv_i2c_writes(AW8692X_REG_CONTCFG8, &drv1_time, AW_I2C_BYTE_ONE); */
	/* DRV2_TIME */
	AW86927_write_str(AW8692X_REG_CONTCFG9, &drv2_time, AW_I2C_BYTE_ONE);
	/* cont play go */
	aw8692x_play_go(AW_TRUE);
}

int aw8692x_f0_default_set(uint16_t f0_pre_val)
{
	if (f0_pre_val == 0) {
		AW_LOGE("err f0_pre:%d", f0_pre_val);
		return AW_ERROR;
	}
	aw8692x_dts.f0_pre = f0_pre_val;
	AW_LOGI("enter f0_pre:%d", f0_pre_val);
	return AW_SUCCESS;
}

int aw8692x_read_f0(void)
{
	uint8_t reg_val[2] = {0};
	uint32_t f0_reg = 0;

#ifdef AW_LRA_F0_DEFAULT
	/* lra_f0 */
	AW86927_read_str(AW8692X_REG_CONTCFG14, reg_val, AW_I2C_BYTE_TWO);
	f0_reg = (reg_val[0] << 8) | reg_val[1];
	if (!f0_reg) {
		g_haptic_hv->f0 = 0;
		AW_LOGE("lra_f0 is error, f0_reg=0");
		return AW_ERROR;
	}
	g_haptic_hv->f0 = (uint32_t)AW8692X_F0_FORMULA(f0_reg);
	AW_LOGI("lra_f0=%d", g_haptic_hv->f0);
#else
	/* cont_f0 */
	AW86927_read_str(AW8692X_REG_CONTCFG16, reg_val, AW_I2C_BYTE_TWO);
	f0_reg = (reg_val[0] << 8) | reg_val[1];
	if (!f0_reg) {
		g_haptic_hv->f0 = 0;
		AW_LOGE("cont_f0 is error, f0_reg=0");
		return AW_ERROR;
	}
	g_haptic_hv->f0 = (uint32_t)AW8692X_F0_FORMULA(f0_reg);
	AW_LOGI("lra_f0=%d", g_haptic_hv->f0);
#endif
	return AW_SUCCESS;
}

void aw8692x_upload_lra(aw_cali_lra cali_type)
{
	uint8_t reg_val = 0;

	aw8692x_reg_unlock(AW_TRUE);
	switch (cali_type) {
	case AW_WRITE_ZERO:
		AW_LOGI("write zero to trim_lra!");
		reg_val = 0x00;
		AW86927_write_str(AW8692X_REG_ANACFG20, &reg_val, AW_I2C_BYTE_ONE);
		break;
	case AW_F0_CALI_LRA:
		AW_LOGI("write f0_cali_data to trim_lra = 0x%02x", g_haptic_hv->f0_cali_data);
		reg_val = (char)g_haptic_hv->f0_cali_data & AW8692X_BIT_ANACFG20_TRIM_LRA;
		AW86927_write_str(AW8692X_REG_ANACFG20, &reg_val, AW_I2C_BYTE_ONE);
		break;
	case AW_OSC_CALI_LRA:
		AW_LOGI("write osc_cali_data to trim_lra = 0x%02X", g_haptic_hv->osc_cali_data);
		reg_val = (char)g_haptic_hv->osc_cali_data & AW8692X_BIT_ANACFG20_TRIM_LRA;
		AW86927_write_str(AW8692X_REG_ANACFG20, &reg_val, AW_I2C_BYTE_ONE);
		break;
	default:
		AW_LOGE("error param!");
		break;
	}
	aw8692x_reg_unlock(AW_FALSE);
}

void aw8692x_auto_bst_enable(aw_bool flag)
{
	g_haptic_hv->auto_bst = flag;
	if (flag) {
		haptic_hv_i2c_write_bits(AW8692X_REG_PLAYCFG3, AW8692X_BIT_PLAYCFG3_AUTO_BST_MASK,
								 AW8692X_BIT_PLAYCFG3_AUTO_BST_ENABLE);
	} else {
		haptic_hv_i2c_write_bits(AW8692X_REG_PLAYCFG3, AW8692X_BIT_PLAYCFG3_AUTO_BST_MASK,
								 AW8692X_BIT_PLAYCFG3_AUTO_BST_DISABLE);
	}
}

void aw8692x_vbat_mode_config(aw_cont_vbat_comp flag)
{
	if (flag == AW_CONT_VBAT_HW_COMP_MODE) {
		haptic_hv_i2c_write_bits(AW8692X_REG_VBATCTRL, AW8692X_BIT_VBATCTRL_VBAT_MODE_MASK,
								 AW8692X_BIT_VBATCTRL_VBAT_MODE_HW);
	} else {
		haptic_hv_i2c_write_bits(AW8692X_REG_VBATCTRL, AW8692X_BIT_VBATCTRL_VBAT_MODE_MASK,
								 AW8692X_BIT_VBATCTRL_VBAT_MODE_SW);
	}
}

void aw8692x_get_vbat(void)
{
	uint8_t reg_val[2] = {0};
	uint32_t vbat_code = 0;

	aw8692x_play_stop();
	aw8692x_raminit_enable(AW_TRUE);
	haptic_hv_i2c_write_bits(AW8692X_REG_DETCFG2, AW8692X_BIT_DETCFG2_DET_SEQ0_MASK, AW8692X_BIT_DETCFG2_DET_SEQ0_VBAT);
	haptic_hv_i2c_write_bits(AW8692X_REG_DETCFG1, AW8692X_BIT_DETCFG1_DET_GO_MASK, AW8692X_BIT_DETCFG1_DET_GO_DET_SEQ0);
	haptic_hv_mdelay(20);
	haptic_hv_i2c_write_bits(AW8692X_REG_DETCFG1, AW8692X_BIT_DETCFG1_DET_GO_MASK, AW8692X_BIT_DETCFG1_DET_GO_NA);

	AW86927_read_str(AW8692X_REG_DETRD1, reg_val, AW_I2C_BYTE_TWO);
	vbat_code = ((reg_val[0] & 0x03) * 256);
	vbat_code = vbat_code + reg_val[1];

	g_haptic_hv->vbat = AW8692X_VBAT_FORMULA(vbat_code);
	if (g_haptic_hv->vbat > AW8692X_VBAT_MAX) {
		g_haptic_hv->vbat = AW8692X_VBAT_MAX;
		AW_LOGI("vbat max limit = %dmV", g_haptic_hv->vbat);
	}
	if (g_haptic_hv->vbat < AW_VBAT_MIN) {
		g_haptic_hv->vbat = AW_VBAT_MIN;
		AW_LOGI("vbat min limit = %dmV", g_haptic_hv->vbat);
	}
	AW_LOGI("g_haptic_hv->vbat=%dmV, vbat_code=0x%02X", g_haptic_hv->vbat, vbat_code);
	aw8692x_raminit_enable(AW_FALSE);
}

int aw8692x_select_d2sgain(uint8_t reg_val)
{
	switch(reg_val) {
	case AW8692X_BIT_DETCFG2_D2S_GAIN_1:
		return 1;
	case AW8692X_BIT_DETCFG2_D2S_GAIN_2:
		return 2;
	case AW8692X_BIT_DETCFG2_D2S_GAIN_4:
		return 4;
	case AW8692X_BIT_DETCFG2_D2S_GAIN_8:
		return 8;
	case AW8692X_BIT_DETCFG2_D2S_GAIN_10:
		return 10;
	case AW8692X_BIT_DETCFG2_D2S_GAIN_16:
		return 16;
	case AW8692X_BIT_DETCFG2_D2S_GAIN_20:
		return 20;
	case AW8692X_BIT_DETCFG2_D2S_GAIN_40:
		return 40;
	default:
		AW_LOGE("error param");
		return AW_ERROR;
	}
}

void aw8692x_get_lra_resistance(void)
{
	uint8_t reg_val[2] = {0};
	uint8_t adc_fs_default = 0;
	uint32_t lra_code = 0;
	uint32_t d2s_gain = 0;

	aw8692x_raminit_enable(AW_TRUE);
	aw8692x_play_stop();
	haptic_hv_mdelay(2);
	haptic_hv_i2c_write_bits(AW8692X_REG_DETCFG2, AW8692X_BIT_DETCFG2_DET_SEQ0_MASK, AW8692X_BIT_DETCFG2_DET_SEQ0_RL);
	AW86927_read_str(AW8692X_REG_DETCFG1, &reg_val[0], AW_I2C_BYTE_ONE);
	adc_fs_default = reg_val[0] & 0x0C;
/* 	haptic_hv_i2c_write_bits(AW8692X_REG_DETCFG1, AW8692X_BIT_DETCFG1_ADC_FS_MASK, AW8692X_BIT_DETCFG1_ADC_FS_12KHZ); */
	haptic_hv_i2c_write_bits(AW8692X_REG_DETCFG1, AW8692X_BIT_DETCFG1_DET_GO_MASK, AW8692X_BIT_DETCFG1_DET_GO_DET_SEQ0);
	haptic_hv_mdelay(3);
	haptic_hv_i2c_write_bits(AW8692X_REG_DETCFG1, AW8692X_BIT_DETCFG1_DET_GO_MASK, AW8692X_BIT_DETCFG1_DET_GO_NA);
	/* restore default config*/
	aw8692x_raminit_enable(AW_FALSE);
	haptic_hv_i2c_write_bits(AW8692X_REG_DETCFG1, AW8692X_BIT_DETCFG1_ADC_FS_MASK, adc_fs_default);
	AW86927_read_str(AW8692X_REG_DETCFG2, &reg_val[0], AW_I2C_BYTE_ONE);
	reg_val[0] &= 0x07;
	d2s_gain = aw8692x_select_d2sgain(reg_val[0]);

	AW86927_read_str(AW8692X_REG_DETRD1, reg_val, AW_I2C_BYTE_TWO);
	lra_code = ((reg_val[0] & 0x03) * 256);
	lra_code = lra_code + reg_val[1];
	g_haptic_hv->lra = AW8692X_LRA_FORMULA(lra_code, d2s_gain);
/* 	haptic_hv_i2c_write_bits(AW8692X_REG_SYSCTRL7, AW8692X_BIT_SYSCTRL7_D2S_GAIN_MASK, d2s_gain_temp); */
	AW_LOGI("Lra resistance value is %d", g_haptic_hv->lra);
}

void aw8692x_set_bst_vol(uint32_t bst_vol)
{
	uint8_t reg_val = 0;

	if (bst_vol < AW8692X_BST_VOL_MIN)
		bst_vol = AW8692X_BST_VOL_MIN;
	else if (bst_vol > AW8692X_BST_VOL_MAX)
		bst_vol = AW8692X_BST_VOL_MAX;
	reg_val = AW8692X_BST_VOL_FORMULA(bst_vol);
	haptic_hv_i2c_write_bits(AW8692X_REG_PLAYCFG1, AW8692X_BIT_PLAYCFG1_BST_VOUT_VREFSET_MASK, reg_val);
}

void aw8692x_set_bst_peak_cur(void)
{
	aw8692x_reg_unlock(AW_TRUE);
	switch (g_haptic_hv->bst_pc) {
	case AW_BST_PC_L1:
		AW_LOGI("bst pc = L1");
		haptic_hv_i2c_write_bits(AW8692X_REG_ANACFG13, AW8692X_BIT_ANACFG13_BST_PC_MASK,
								 AW8692X_BIT_ANACFG13_BST_PEAKCUR_3P45A);
		break;
	case AW_BST_PC_L2:
		AW_LOGI("bst pc = L2");
		haptic_hv_i2c_write_bits(AW8692X_REG_ANACFG13, AW8692X_BIT_ANACFG13_BST_PC_MASK,
								 AW8692X_BIT_ANACFG13_BST_PEAKCUR_4A);
		break;
	default:
		AW_LOGI("bst pc = L1");
		haptic_hv_i2c_write_bits(AW8692X_REG_ANACFG13, AW8692X_BIT_ANACFG13_BST_PC_MASK,
								 AW8692X_BIT_ANACFG13_BST_PEAKCUR_3P45A);
		break;
	}
	aw8692x_reg_unlock(AW_FALSE);
}

void aw8692x_set_wav_seq(uint8_t wav, uint8_t seq)
{
	AW86927_write_str(AW8692X_REG_WAVCFG1 + wav, &seq, AW_I2C_BYTE_ONE);
}

void aw8692x_set_wav_loop(uint8_t wav, uint8_t loop)
{
	uint8_t tmp = 0;

	if (wav % 2) {
		tmp = loop << 0;
		haptic_hv_i2c_write_bits(AW8692X_REG_WAVCFG9 + (wav / 2), AW8692X_BIT_WAVLOOP_SEQ_EVEN_MASK, tmp);
	} else {
		tmp = loop << 4;
		haptic_hv_i2c_write_bits(AW8692X_REG_WAVCFG9 + (wav / 2), AW8692X_BIT_WAVLOOP_SEQ_ODD_MASK, tmp);
	}
}

void aw8692x_set_main_loop(uint8_t loop)
{
	haptic_hv_i2c_write_bits(AW8692X_REG_WAVCFG13, AW8692X_BIT_WAVCFG13_MAINLOOP_MASK, loop);
}

void aw8692x_set_gain(uint8_t gain)
{
	g_haptic_hv->gain = gain;
	AW86927_write_str(AW8692X_REG_PLAYCFG2, &gain, AW_I2C_BYTE_ONE);
}

uint8_t aw8692x_rtp_get_fifo_afs(void)
{
	uint8_t reg_val = 0;

	AW86927_read_str(AW8692X_REG_SYSST, &reg_val, AW_I2C_BYTE_ONE);
	reg_val &= AW8692X_BIT_SYSST_FF_AFS;
	reg_val = reg_val >> 3;
	return reg_val;
}

void aw8692x_set_rtp_aei(aw_bool flag)
{
	if (flag) {
		haptic_hv_i2c_write_bits(AW8692X_REG_SYSINTM, AW8692X_BIT_SYSINTM_FF_AEM_MASK, AW8692X_BIT_SYSINTM_FF_AEM_ON);
	} else {
		haptic_hv_i2c_write_bits(AW8692X_REG_SYSINTM, AW8692X_BIT_SYSINTM_FF_AEM_MASK, AW8692X_BIT_SYSINTM_FF_AEM_OFF);
	}
}

void aw8692x_set_base_addr(void)
{
	uint8_t reg_val[5] = {0};
	uint32_t temp = 0;

	/* RAM Base Addr H */
	reg_val[0] = (uint8_t)(g_haptic_hv->base_addr >> 8);
	/* RAM Base Addr L */
	reg_val[1] = (uint8_t)(g_haptic_hv->base_addr & 0xFF);
	/* FIFO AEH & AFH */
	reg_val[2] = (uint8_t)(AW8692X_FIFO_AE_ADDR_H(g_haptic_hv->base_addr) | AW8692X_FIFO_AF_ADDR_H(g_haptic_hv->base_addr));
	/* FIFO AEL */
	reg_val[3] = (uint8_t)(AW8692X_FIFO_AE_ADDR_L(g_haptic_hv->base_addr));
	/* FIFO AFL */
	reg_val[4] = (uint8_t)(AW8692X_FIFO_AF_ADDR_L(g_haptic_hv->base_addr));
	AW86927_write_str(AW8692X_REG_RTPCFG1, reg_val, AW_I2C_BYTE_FIVE);

	AW86927_read_str(AW8692X_REG_RTPCFG3, reg_val, AW_I2C_BYTE_THREE);
	temp = ((reg_val[0] & 0x0f) << 24) | ((reg_val[0] & 0xf0) << 4);
	temp = temp | reg_val[1];
	AW_LOGI("almost_empty_threshold = %d", (temp & 0x0000ffff));
	temp = temp | (reg_val[2] << 16);
	AW_LOGI("almost_full_threshold = %d", temp >> 16);
}

void aw8692x_set_ram_addr(void)
{
	uint8_t reg_val[2] = {0};

	reg_val[0] = (uint8_t)(g_haptic_hv->base_addr >> 8);
	reg_val[1] = (uint8_t)(g_haptic_hv->base_addr & 0xFF);
	AW86927_write_str(AW8692X_REG_RAMADDRH, reg_val, AW_I2C_BYTE_TWO);
}

void aw8692x_set_ram_data(uint8_t *d123, uint32_t len)
{
	AW86927_write_str(AW8692X_REG_RAMDATA, d123, len);
}

void aw8692x_get_ram_data(uint8_t *d123, uint32_t len)
{
	AW86927_read_str(AW8692X_REG_RAMDATA, d123, len);
}

void aw8692x_set_rtp_data(uint8_t *d123, uint32_t len)
{
	AW86927_write_str(AW8692X_REG_RTPDATA, d123, len);
}

void aw8692x_irq_clear(void)
{
	uint8_t reg_val = 0;

	AW86927_read_str(AW8692X_REG_SYSINT, &reg_val, AW_I2C_BYTE_ONE);
	AW_LOGI("reg SYSINT=0x%x", reg_val);
}

void aw8692x_interrupt_setup(void)
{
	uint8_t reg_val = 0;

	AW_LOGI("enter");
	AW86927_read_str(AW8692X_REG_SYSINT, &reg_val, AW_I2C_BYTE_ONE);
	/* edge int mode */
	haptic_hv_i2c_write_bits(AW8692X_REG_SYSCTRL4,
							 (AW8692X_BIT_SYSCTRL4_INT_MODE_MASK & AW8692X_BIT_SYSCTRL4_INT_EDGE_MODE_MASK),
							 (AW8692X_BIT_SYSCTRL4_INT_MODE_EDGE | AW8692X_BIT_SYSCTRL4_INT_EDGE_MODE_POS));

	/* int enable */
	haptic_hv_i2c_write_bits(AW8692X_REG_SYSINTM, (AW8692X_BIT_SYSINTM_BST_SCPM_MASK &
							 AW8692X_BIT_SYSINTM_BST_OVPM_MASK & AW8692X_BIT_SYSINTM_UVLM_MASK &
							 AW8692X_BIT_SYSINTM_OCDM_MASK & AW8692X_BIT_SYSINTM_OTM_MASK),
							 (AW8692X_BIT_SYSINTM_BST_SCPM_ON | AW8692X_BIT_SYSINTM_BST_OVPM_OFF |
							 AW8692X_BIT_SYSINTM_UVLM_ON | AW8692X_BIT_SYSINTM_OCDM_ON | AW8692X_BIT_SYSINTM_OTM_ON));
}

void aw8692x_trig1_param_config(void)
{
	uint8_t trig_config = 0;

	AW_LOGI("enter");
	if (aw8692x_trig1.trig_level)
		trig_config |= AW8692X_BIT_TRGCFG7_TRG1_MODE_LEVEL;
	else
		trig_config |= AW8692X_BIT_TRGCFG7_TRG1_MODE_EDGE;

	if (aw8692x_trig1.trig_polar)
		trig_config |= AW8692X_BIT_TRGCFG7_TRG1_POLAR_NEG;
	else
		trig_config |= AW8692X_BIT_TRGCFG7_TRG1_POLAR_POS;

	if (aw8692x_trig1.trig_brk)
		trig_config |= AW8692X_BIT_TRGCFG7_TRG1_AUTO_BRK_ENABLE;
	else
		trig_config |= AW8692X_BIT_TRGCFG7_TRG1_AUTO_BRK_DISABLE;

	if (aw8692x_trig1.trig_bst)
		trig_config |= AW8692X_BIT_TRGCFG7_TRG1_BST_ENABLE;
	else
		trig_config |= AW8692X_BIT_TRGCFG7_TRG1_BST_DISABLE;

	haptic_hv_i2c_write_bits(AW8692X_REG_TRGCFG7, (AW8692X_BIT_TRGCFG7_TRG1_MODE_MASK &
							 AW8692X_BIT_TRGCFG7_TRG1_POLAR_MASK & AW8692X_BIT_TRGCFG7_TRG1_AUTO_BRK_MASK &
							 AW8692X_BIT_TRGCFG7_TRG1_BST_MASK), trig_config);

	trig_config = 0;
	/* pos config */
	if (aw8692x_trig1.pos_enable)
		trig_config |= AW8692X_BIT_TRG_ENABLE;
	else
		trig_config |= AW8692X_BIT_TRG_DISABLE;

	trig_config |= aw8692x_trig1.pos_sequence;
	AW86927_write_str(AW8692X_REG_TRGCFG1, &trig_config, AW_I2C_BYTE_ONE);

	trig_config = 0;

	/* neq config */
	if (aw8692x_trig1.neg_enable)
		trig_config |= AW8692X_BIT_TRG_ENABLE;
	else
		trig_config |= AW8692X_BIT_TRG_DISABLE;

	trig_config |= aw8692x_trig1.neg_sequence;
	AW86927_write_str(AW8692X_REG_TRGCFG4, &trig_config, AW_I2C_BYTE_ONE);
}

void aw8692x_trig2_param_config(void)
{
	uint8_t trig_config = 0;

	AW_LOGI("enter");
	if (aw8692x_trig2.trig_level)
		trig_config |= AW8692X_BIT_TRGCFG7_TRG2_MODE_LEVEL;
	else
		trig_config |= AW8692X_BIT_TRGCFG7_TRG2_MODE_EDGE;

	if (aw8692x_trig2.trig_polar)
		trig_config |= AW8692X_BIT_TRGCFG7_TRG2_POLAR_NEG;
	else
		trig_config |= AW8692X_BIT_TRGCFG7_TRG2_POLAR_POS;

	if (aw8692x_trig2.trig_brk)
		trig_config |= AW8692X_BIT_TRGCFG7_TRG2_AUTO_BRK_ENABLE;
	else
		trig_config |= AW8692X_BIT_TRGCFG7_TRG2_AUTO_BRK_DISABLE;

	if (aw8692x_trig2.trig_bst)
		trig_config |= AW8692X_BIT_TRGCFG7_TRG2_BST_ENABLE;
	else
		trig_config |= AW8692X_BIT_TRGCFG7_TRG2_BST_DISABLE;

	haptic_hv_i2c_write_bits(AW8692X_REG_TRGCFG7, (AW8692X_BIT_TRGCFG7_TRG2_MODE_MASK &
							 AW8692X_BIT_TRGCFG7_TRG2_POLAR_MASK & AW8692X_BIT_TRGCFG7_TRG2_AUTO_BRK_MASK &
							 AW8692X_BIT_TRGCFG7_TRG2_BST_MASK), trig_config);

	trig_config = 0;

	/* pos config */
	if (aw8692x_trig2.pos_enable)
		trig_config |= AW8692X_BIT_TRG_ENABLE;
	else
		trig_config |= AW8692X_BIT_TRG_DISABLE;

	trig_config |= aw8692x_trig2.pos_sequence;
	AW86927_write_str(AW8692X_REG_TRGCFG2, &trig_config, AW_I2C_BYTE_ONE);

	trig_config = 0;

	/* neq config */
	if (aw8692x_trig2.neg_enable)
		trig_config |= AW8692X_BIT_TRG_ENABLE;
	else
		trig_config |= AW8692X_BIT_TRG_DISABLE;

	trig_config |= aw8692x_trig2.neg_sequence;
	AW86927_write_str(AW8692X_REG_TRGCFG5, &trig_config, AW_I2C_BYTE_ONE);
}

void aw8692x_trig3_param_config(void)
{
	uint8_t trig_config = 0;

	AW_LOGI("enter");
	if (aw8692x_trig3.trig_level)
		trig_config |= AW8692X_BIT_TRGCFG8_TRG3_MODE_LEVEL;
	else
		trig_config |= AW8692X_BIT_TRGCFG8_TRG3_MODE_EDGE;

	if (aw8692x_trig3.trig_polar)
		trig_config |= AW8692X_BIT_TRGCFG8_TRG3_POLAR_NEG;
	else
		trig_config |= AW8692X_BIT_TRGCFG8_TRG3_POLAR_POS;

	if (aw8692x_trig3.trig_brk)
		trig_config |= AW8692X_BIT_TRGCFG8_TRG3_AUTO_BRK_ENABLE;
	else
		trig_config |= AW8692X_BIT_TRGCFG8_TRG3_AUTO_BRK_DISABLE;

	if (aw8692x_trig3.trig_bst)
		trig_config |= AW8692X_BIT_TRGCFG8_TRG3_BST_ENABLE;
	else
		trig_config |= AW8692X_BIT_TRGCFG8_TRG3_BST_DISABLE;

	haptic_hv_i2c_write_bits(AW8692X_REG_TRGCFG8, (AW8692X_BIT_TRGCFG8_TRG3_MODE_MASK &
							 AW8692X_BIT_TRGCFG8_TRG3_POLAR_MASK & AW8692X_BIT_TRGCFG8_TRG3_AUTO_BRK_MASK &
							 AW8692X_BIT_TRGCFG8_TRG3_BST_MASK), trig_config);

	trig_config = 0;

	/* pos config */
	if (aw8692x_trig3.pos_enable)
		trig_config |= AW8692X_BIT_TRG_ENABLE;
	else
		trig_config |= AW8692X_BIT_TRG_DISABLE;

	trig_config |= aw8692x_trig3.pos_sequence;
	AW86927_write_str(AW8692X_REG_TRGCFG3, &trig_config, AW_I2C_BYTE_ONE);

	trig_config = 0;
	/* neq config */
	if (aw8692x_trig3.neg_enable)
		trig_config |= AW8692X_BIT_TRG_ENABLE;
	else
		trig_config |= AW8692X_BIT_TRG_DISABLE;

	trig_config |= aw8692x_trig3.neg_sequence;
	AW86927_write_str(AW8692X_REG_TRGCFG6, &trig_config, AW_I2C_BYTE_ONE);
}

void aw8692x_onewire_mode_cfg(void)
{
	uint8_t start_delay = 0x75;

	AW_LOGI("enter");
	haptic_hv_i2c_write_bits(AW8692X_REG_PLAYCFG3, AW8692X_BIT_PLAYCFG3_ONEWIRE_COMP_MASK,
							 AW8692X_BIT_PLAYCFG3_1908_ONEWIRE_MODE);
	haptic_hv_i2c_write_bits(AW8692X_REG_TRGCFG8, AW8692X_BIT_TRGCFG8_TRG_ONEWIRE_MASK,
							 AW8692X_BIT_TRGCFG8_TRG_ONEWIRE_ENABLE);
	AW86927_write_str(AW8692X_REG_GLBCFG2, &start_delay, AW_I2C_BYTE_ONE);
}

void aw8692x_trig_mode_cfg(void)
{
	if (aw8692x_dts.enabled_one_wire) {
		AW_LOGI("one wire is enabled!");
		aw8692x_onewire_mode_cfg();
	} else {
		aw8692x_trig1_param_config();
	}
	aw8692x_trig2_param_config();
	aw8692x_trig3_param_config();
}

int aw8692x_get_irq_state(void)
{
	int ret = 0;
	uint8_t reg_val = 0;

	AW86927_read_str(AW8692X_REG_SYSINT, &reg_val, AW_I2C_BYTE_ONE);
	if (reg_val & AW8692X_BIT_SYSINT_BST_SCPI) {
		ret = AW_IRQ_BST_SCP;
		AW_LOGE("chip scp int error");
	}

	if (reg_val & AW8692X_BIT_SYSINT_BST_OVPI) {
		ret = AW_IRQ_BST_OVP;
		AW_LOGE("chip ov int error");
	}

	if (reg_val & AW8692X_BIT_SYSINT_UVLI) {
		ret = AW_IRQ_UVLO;
		AW_LOGE("chip uvlo int error");
	}

	if (reg_val & AW8692X_BIT_SYSINT_OCDI) {
		ret = AW_IRQ_OCD;
		AW_LOGE("chip over current int error");
	}

	if (reg_val & AW8692X_BIT_SYSINT_OTI) {
		ret = AW_IRQ_OT;
		AW_LOGE("chip over temperature int error");
	}

	if (reg_val & AW8692X_BIT_SYSINT_DONEI) {
		ret = AW_IRQ_DONE;
		AW_LOGI("chip playback done");
	}

	if (reg_val & AW8692X_BIT_SYSINT_FF_AFI) {
		ret = AW_IRQ_ALMOST_FULL;
		AW_LOGI("haptic_hv rtp mode fifo almost full!");
	}

	if (reg_val & AW8692X_BIT_SYSINT_FF_AEI)
		ret = AW_IRQ_ALMOST_EMPTY;

	return ret;
}

void aw8692x_protect_config(uint8_t prtime, uint8_t prlvl)
{
	uint8_t reg_val = 0;

	haptic_hv_i2c_write_bits(AW8692X_REG_PWMCFG1, AW8692X_BIT_PWMCFG1_PRC_EN_MASK,
							 AW8692X_BIT_PWMCFG1_PRC_DISABLE);
	if (prlvl != 0) {
		/* Enable protection mode */
		AW_LOGI("enable protection mode");
		reg_val = AW8692X_BIT_PWMCFG3_PR_ENABLE | (prlvl & (~AW8692X_BIT_PWMCFG3_PRLVL_MASK));
		AW86927_write_str(AW8692X_REG_PWMCFG3, &reg_val, AW_I2C_BYTE_ONE);
		AW86927_write_str(AW8692X_REG_PWMCFG4, &prtime, AW_I2C_BYTE_ONE);
	} else {
		/* Disable */
		AW_LOGI("disable protection mode");
		haptic_hv_i2c_write_bits(AW8692X_REG_PWMCFG3, AW8692X_BIT_PWMCFG3_PR_EN_MASK,
								 AW8692X_BIT_PWMCFG3_PR_DISABLE);
	}
}

int aw8692x_get_f0(void)
{
	uint8_t reg_val = 0;
	uint8_t brk_en_default = 0;
	uint8_t cont_config[4] = {0};
	uint8_t d2s_gain_default = 0;
	int drv_width = 0;
	int ret = AW_SUCCESS;

	g_haptic_hv->f0 = aw8692x_dts.f0_pre;
	/* enter standby mode */
	aw8692x_play_stop();
	/* config max d2s_gain */
	AW86927_read_str(AW8692X_REG_DETCFG2, &reg_val, AW_I2C_BYTE_ONE);
	d2s_gain_default = reg_val & AW8692X_BIT_DETCFG2_D2S_GAIN;
	haptic_hv_i2c_write_bits(AW8692X_REG_DETCFG2, AW8692X_BIT_DETCFG2_D2S_GAIN_MASK, AW8692X_BIT_DETCFG2_D2S_GAIN_40);
	haptic_hv_i2c_write_bits(AW8692X_REG_CONTCFG1, (AW8692X_BIT_TRIMCFG1_RL_TRIM_SRC_MASK &
							 AW8692X_BIT_CONTCFG1_EN_F0_DET_MASK), (AW8692X_BIT_TRIMCFG1_RL_TRIM_SRC_REG |
							 AW8692X_BIT_CONTCFG1_F0_DET_ENABLE));
	/* enable auto break */
	AW86927_read_str(AW8692X_REG_PLAYCFG3, &reg_val, AW_I2C_BYTE_ONE);
	brk_en_default = 0x04 & reg_val;
	aw8692x_auto_brake_mode(AW_TRUE);
#ifdef AW_RAM_GET_F0
	if (g_haptic_hv->ram_num < AW_RAM_GET_F0_SEQ) {
		AW_LOGE("miss ram get f0 waveform!");
		return AW_ERROR;
	}
	/* f0 calibrate work mode */
	aw8692x_play_mode(AW_RAM_MODE);
	aw8692x_set_bst_vol(9000);
	aw8692x_set_wav_seq(0x00, AW_RAM_GET_F0_SEQ);
	aw8692x_set_wav_seq(0x01, 0x00);
	aw8692x_set_gain(AW_GAIN_LEVEL_1);
	aw8692x_set_wav_loop(0x00, AW_LOOP2);
#else
	/* f0 calibrate work mode */
	aw8692x_play_mode(AW_CONT_MODE);
	/* cont config */
	cont_config[0] = aw8692x_dts.cont_drv1_lvl | AW8692X_BIT_CONTCFG6_TRACK_ENABLE;
	cont_config[1] = aw8692x_dts.cont_drv2_lvl;
	cont_config[2] = aw8692x_dts.cont_drv1_time;
	cont_config[3] = aw8692x_dts.cont_drv2_time;
	AW86927_write_str(AW8692X_REG_CONTCFG6, cont_config, AW_I2C_BYTE_FOUR);

	/* TRACK_MARGIN */
	if (!aw8692x_dts.cont_track_margin){
		AW_LOGE("aw8692x_dts.cont_track_margin = 0");
	}else
		AW86927_write_str(AW8692X_REG_CONTCFG11, &aw8692x_dts.cont_track_margin, AW_I2C_BYTE_ONE);
	/* DRV_WIDTH */
	if (!aw8692x_dts.f0_pre)
		return AW_ERROR;
	drv_width = AW_DRV_WIDTH_FORMULA(aw8692x_dts.f0_pre, aw8692x_dts.cont_brk_gain, aw8692x_dts.cont_track_margin);
	if (drv_width < AW_DRV_WIDTH_MIN)
		drv_width = AW_DRV_WIDTH_MIN;
	if (drv_width > AW_DRV_WIDTH_MAX)
		drv_width = AW_DRV_WIDTH_MAX;
	cont_config[0] = (uint8_t)drv_width;
	AW86927_write_str(AW8692X_REG_CONTCFG3, &cont_config[0], AW_I2C_BYTE_ONE);
#endif
	/* cont play go */
	aw8692x_play_go(AW_TRUE);
	haptic_hv_mdelay(20);
	aw8692x_wait_enter_standby();
	ret = aw8692x_read_f0();
	/* restore d2s_gain config */
	haptic_hv_i2c_write_bits(AW8692X_REG_DETCFG2, AW8692X_BIT_DETCFG2_D2S_GAIN_MASK, d2s_gain_default);
	/* restore default config */
	haptic_hv_i2c_write_bits(AW8692X_REG_CONTCFG1, AW8692X_BIT_CONTCFG1_EN_F0_DET_MASK,
							 AW8692X_BIT_CONTCFG1_F0_DET_DISABLE);
	/* recover auto break config */
	if (brk_en_default)
		aw8692x_auto_brake_mode(AW_TRUE);
	else
		aw8692x_auto_brake_mode(AW_FALSE);
	
	aw8692x_set_bst_vol(9000);//set bst to the target
	return ret;
}

void aw8692x_misc_para_init(void)
{
	uint8_t reg_val = 0;
	uint32_t drv2_lvl = 0;

	/* Set gain_bypass */
	haptic_hv_i2c_write_bits(AW8692X_REG_SYSCTRL4, AW8692X_BIT_SYSCTRL4_GAIN_BYPASS_MASK, aw8692x_dts.gain_bypass);
	drv2_lvl = AW8692X_DRV2_LVL_FORMULA(aw8692x_dts.f0_pre, aw8692x_dts.cont_lra_vrms);
	if (drv2_lvl > AW_DRV2_LVL_MAX)
		aw8692x_dts.cont_drv2_lvl = AW_DRV2_LVL_MAX;
	else
		aw8692x_dts.cont_drv2_lvl = (uint8_t)drv2_lvl;
	aw8692x_reg_unlock(AW_TRUE);

	reg_val = AW8692X_BIT_ANACFG11_INIT_VAL;
	AW86927_write_str(AW8692X_REG_ANACFG11, &reg_val, AW_I2C_BYTE_ONE);

	/* Close boost skip */
	haptic_hv_i2c_write_bits(AW8692X_REG_ANACFG12, AW8692X_BIT_ANACFG12_BST_SKIP_MASK,
							 AW8692X_BIT_ANACFG12_BST_SKIP_SHUTDOWN);

	/* Open adaptive ipeak current limiting */
	haptic_hv_i2c_write_bits(AW8692X_REG_ANACFG15, AW8692X_BIT_ANACFG15_BST_PEAK_MODE_MASK,
							 AW8692X_BIT_ANACFG15_BST_PEAK_BACK);

	haptic_hv_i2c_write_bits(AW8692X_REG_ANACFG16, AW8692X_BIT_ANACFG16_BST_SRC_MASK, AW8692X_BIT_ANACFG16_BST_SRC_3NS);

	aw8692x_reg_unlock(AW_FALSE);

	reg_val = g_haptic_hv->i2c_addr >> 1;
	AW86927_write_str(AW8692X_REG_SYSCTRL5, &reg_val, AW_I2C_BYTE_ONE);

	/* d2s_gain */
	if (!aw8692x_dts.d2s_gain) {
		AW_LOGE("aw8692x_dts.d2s_gain = 0!");
	} else {
		haptic_hv_i2c_write_bits(AW8692X_REG_DETCFG2, AW8692X_BIT_DETCFG2_D2S_GAIN_MASK, aw8692x_dts.d2s_gain);
	}

	/* cont_tset */
	if (!aw8692x_dts.cont_tset) {
		AW_LOGE("cont_tset = 0!");
	} else {
		haptic_hv_i2c_write_bits(AW8692X_REG_CONTCFG13, AW8692X_BIT_CONTCFG13_TSET_MASK, aw8692x_dts.cont_tset << 4);
	}

	/* cont_bemf_set */
	if (!aw8692x_dts.cont_bemf_set) {
		AW_LOGE("cont_bemf_set = 0!");
	} else {
		haptic_hv_i2c_write_bits(AW8692X_REG_CONTCFG13, AW8692X_BIT_CONTCFG13_BEME_SET_MASK, aw8692x_dts.cont_bemf_set);
	}

	/* cont_brk_time */
	if (!aw8692x_dts.cont_brk_time) {
		AW_LOGE("cont_brk_time = 0!");
	} else {
		AW86927_write_str(AW8692X_REG_CONTCFG10, &aw8692x_dts.cont_brk_time, AW_I2C_BYTE_ONE);
	}

	/* cont_bst_brk_gain */
	if (!aw8692x_dts.cont_bst_brk_gain) {
		AW_LOGE("cont_bst_brk_gain = 0!");
	} else {
		haptic_hv_i2c_write_bits(AW8692X_REG_CONTCFG5, AW8692X_BIT_CONTCFG5_BST_BRK_GAIN_MASK,
								 aw8692x_dts.cont_bst_brk_gain << 4);
	}

	/* cont_brk_gain */
	if (!aw8692x_dts.cont_brk_gain) {
		AW_LOGE("cont_brk_gain = 0!");
	} else {
		haptic_hv_i2c_write_bits(AW8692X_REG_CONTCFG5, AW8692X_BIT_CONTCFG5_BRK_GAIN_MASK, aw8692x_dts.cont_brk_gain);
	}

	/* bst_vol set to 8.5v default */
	aw8692x_set_bst_vol(9000); // fanlong
	aw8692x_set_bst_peak_cur();
	aw8692x_protect_config(AW8692X_BIT_PWMCFG4_PRTIME_DEFAULT_VALUE, AW8692X_BIT_PWMCFG3_PRLVL_DEFAULT_VALUE);
}

void aw8692x_reg_show(void)
{
	uint8_t i = 0;
	uint8_t reg_array[AW8692X_REG_ANACFG22 + 1] = {0};

	AW86927_read_str(AW8692X_REG_RSTCFG, reg_array, AW8692X_REG_RTPDATA);
	AW86927_read_str((AW8692X_REG_RTPDATA + 1), &reg_array[AW8692X_REG_RTPDATA + 1],
						(AW8692X_REG_RAMDATA - AW8692X_REG_RTPDATA - 1));
	AW86927_read_str((AW8692X_REG_RAMDATA + 1), &reg_array[AW8692X_REG_RAMDATA + 1],
						(AW8692X_REG_ANACFG22 - AW8692X_REG_RAMDATA));
	for (i = 0; i <= AW8692X_REG_ANACFG22; i++)
		if ((i != AW8692X_REG_RTPDATA) && (i != AW8692X_REG_RAMDATA)){
			AW_LOGI("reg:0x%02x=0x%02x", i, reg_array[i]);
		}
}

// struct aw_haptic_func aw8692x_func_list;
//= {
//	.get_f0 = aw8692x_get_f0,
//	.get_irq_state = aw8692x_get_irq_state,
//	.check_qualify = aw8692x_check_qualify,
//	.judge_rtp_going = aw8692x_judge_rtp_going,
//	.f0_default_set = aw8692x_f0_default_set,
//	.play_go = aw8692x_play_go,
//	.set_pwm = aw8692x_set_pwm,
//	.get_vbat = aw8692x_get_vbat,
//	.reg_show = aw8692x_reg_show,
//	.set_gain = aw8692x_set_gain,
//	.play_stop = aw8692x_play_stop,
//	.irq_clear = aw8692x_irq_clear,
//	.play_mode = aw8692x_play_mode,
//	.upload_lra = aw8692x_upload_lra,
//	.cont_config = aw8692x_cont_config,
//	.set_rtp_aei = aw8692x_set_rtp_aei,
//	.set_bst_vol = aw8692x_set_bst_vol,
//	.set_wav_seq = aw8692x_set_wav_seq,
//	.set_wav_loop = aw8692x_set_wav_loop,
//	.set_main_loop = aw8692x_set_main_loop,
//	.set_rtp_data = aw8692x_set_rtp_data,
//	.set_ram_data = aw8692x_set_ram_data,
//	.get_ram_data = aw8692x_get_ram_data,
//	.set_ram_addr = aw8692x_set_ram_addr,
//	.set_base_addr = aw8692x_set_base_addr,
//	.trig_mode_cfg = aw8692x_trig_mode_cfg,
//	.raminit_enable = aw8692x_raminit_enable,
//	.misc_para_init = aw8692x_misc_para_init,
//	.protect_config = aw8692x_protect_config,
//	.interrupt_setup = aw8692x_interrupt_setup,
//	.auto_bst_enable = aw8692x_auto_bst_enable,
//	.auto_brake_mode = aw8692x_auto_brake_mode,
//	.bst_mode_config = aw8692x_bst_mode_config,
//	.onewire_mode_cfg = aw8692x_onewire_mode_cfg,
//	.vbat_mode_config = aw8692x_vbat_mode_config,
//	.get_lra_resistance = aw8692x_get_lra_resistance,
//	.get_glb_state = aw8692x_get_glb_state,
//	.rtp_get_fifo_afs = aw8692x_rtp_get_fifo_afs,
//};
